//
//  ViewController.m
//  JSON解析
//
//  Created by hyl on 16/8/1.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "ViewController.h"
#import "HYLVideo.h"
#import <UIImageView+WebCache.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
/** videoArr*/
@property (nonatomic,strong) NSArray *videoArr;
/** videoDic*/
@property (nonatomic,strong) NSDictionary *videoDic;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self parseJSON];
    [self parseJSon2];
   }

-(NSArray *)videoArr{
    NSMutableArray *MArr=[NSMutableArray array];
    if (_videoArr==nil) {
        
        for (NSDictionary *dic in _videoDic) {
            HYLVideo *video=[HYLVideo videoWithDic:dic];
            [MArr addObject:video];
        }
            
        _videoArr=MArr;
    }else if (_videoArr.count==0)
    {
        for (NSDictionary *dic in _videoDic) {
            HYLVideo *video=[HYLVideo videoWithDic:dic];
            [MArr addObject:video];
        }
        
        _videoArr=MArr;
    }
    return _videoArr;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.videoArr.count;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"video"];
    HYLVideo *video=self.videoArr[indexPath.row];
    NSString *imageURL=[@"http://120.25.226.186:32812" stringByAppendingPathComponent:video.image];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageURL]];
//    cell.imageView.image=[UIImage imageNamed:video.image];
    
    cell.detailTextLabel.text=[NSString stringWithFormat:@"时长：%zd",video.length];
    cell.textLabel.text=video.name;
    return cell;
}

-(void) parseJSON{
    //URＬ
    NSURL *url=[NSURL URLWithString:@"http://120.25.226.186:32812/video"];
    
    //新建一个请求对象
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    //
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        //NSJSONReadingMutableContainers
        //NSJSONReadingMutableLeaves
        //NSJSONReadingAllowFragments
        self.videoDic=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
//        [dic writeToFile:@"/Users/hyl/Desktop/video.plist" atomically:YES];
//        [self.tableView reloadData];
    }];
}

-(void) parseJSon2{
    //URＬ
    NSURL *url=[NSURL URLWithString:@"http://120.25.226.186:32812/video"];
    //新建一个请求对象
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    NSURLSession *session=[NSURLSession sharedSession];
//    [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//        self.videoDic=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
//    }];
    NSURLSessionDataTask *dataTask=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        self.videoDic=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil][@"videos"];
//       NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        //NSLog(@"%@",self.videoDic[@"videos"]);
        [self.tableView reloadData];
    }];
    [dataTask resume];
}

-(void) parseJSon3{
    
    dispatch_queue_t queue=dispatch_get_main_queue();
    
    dispatch_async(queue, ^{
        //URＬ
        NSURL *url=[NSURL URLWithString:@"http://120.25.226.186:32812/video"];
        //新建一个请求对象
        NSURLRequest *request=[NSURLRequest requestWithURL:url];
        NSURLSession *session=[NSURLSession sharedSession];
        //    [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        //        self.videoDic=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        //    }];
        NSURLSessionDataTask *dataTask=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            self.videoDic=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
            NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        }];
        [dataTask resume];

    });
}
#pragma mark - UITableViewDelete
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    HYLVideo *video=self.videoArr[indexPath.row];
    NSString *videoURL=[@"http://120.25.226.186:32812" stringByAppendingPathComponent:video.url];
    MPMoviePlayerViewController *vc=[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString:videoURL]];
    [self presentViewController:vc animated:YES completion:nil];

}
@end
